package cn.tedu.scheduled;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class ScheduledExecutorServiceDemo01 {
    public static void main(String[] args) {
        //1.构建任务执行器对象
        ScheduledExecutorService service =
                Executors.newScheduledThreadPool(3);
        //2.构建任务对象(任务对象可以是Runnable类型，也可以是Callable类型-需要返回值用它)
        Runnable task1 = () -> {
            String threadName = Thread.currentThread().getName();
            System.out.println(threadName+"->task1:"+System.currentTimeMillis());
        };
        //3.执行任务
        //3.1以固定频率执行
        service.scheduleAtFixedRate(task1,
                0,//初始延迟
                1000,//两次任务执行的时间间隔
                   java.util.concurrent.TimeUnit.MILLISECONDS);//时间单位
        //3.2以固定延迟执行(下一次执行时间：delay+任务执行时间)
        //service.scheduleWithFixedDelay(task1,0,1000,java.util.concurrent.TimeUnit.MILLISECONDS);

        try {
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        service.shutdown();
        System.out.println("程序结束");

    }
}
